import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards, SetMetadata } from "@nestjs/common";
import { VideoService } from "./video.service";
import { CreateVideoDto } from "./dto/create-video.dto";
import { UpdateVideoDto } from "./dto/update-video.dto";
import { PaginationVideoDto } from "./dto/pagination-video.dto";
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from "src/auth/roles.guard";

@Controller("/mg/video")
export class VideoController {
    constructor(private readonly videoService: VideoService) { }

    @Post()
    async create(@Body() createVideoDto: CreateVideoDto) {
        let res = await this.videoService.create(createVideoDto);
        this.videoService.downloadImg(createVideoDto);
        return res;
    }

    @UseGuards(AuthGuard)
    @Get()
    findAll(@Query() paginationVideoDto: PaginationVideoDto) {
        return this.videoService.findAll(paginationVideoDto);
    }

    @Get(":id")
    findOne(@Param("id") id: string) {
        return this.videoService.findOne(id);
    }

    @Patch(":id")
    update(@Param("id") id: string, @Body() updateVideoDto: UpdateVideoDto) {
        return this.videoService.update(id, updateVideoDto);
    }

    @UseGuards(AuthGuard, RolesGuard)
    @SetMetadata("roles", ["超级管理员"])
    @Delete(":id")
    remove(@Param("id") id: string) {
        return this.videoService.remove(id);
    }

    @Post("img")
    async getImage(@Body() body: any) {
        console.log(body)
        const name = await this.videoService.downloadImg(body)
        return { name };
    }

    // @UseGuards(AuthGuard, RolesGuard) 
    // @SetMetadata("roles", ["超级管理员"])
    @Post("download")
    async download(@Body() body: any) {
        // video: link,   ...mp4
        // src: url,   /weer554
        // size: arr[0].size   480x1280
        console.log("@@@ download")

        console.log(body)
        const name = await this.videoService.downloadVideo(body);
        return { name };
    }
}

